package divide_and_rule;

/**
 * 颜色分类
 */
public class Demo20250812 {
    public void sortColors(int[] nums) {
        int i = 0, left = -1, right = nums.length;
        while (i < right) {
            if (nums[i] == 0) {
                swap (nums, ++left, i++);
            } else if (nums[i] == 1) {
                i++;
            } else if (nums[i] == 2) {
                swap (nums, --right, i);
            }
        }
    }

    public void swap(int[] nums, int a, int b) {
        int tmp = nums[a];
        nums[a] = nums[b];
        nums[b] = tmp;
    }
}
